/*
 *  Arrow
 *
 *  Created by David Giovannini on 1/5/07.
 *  Copyright 2007 Software by Jove. All rights reserved.
 *
 */
 
#ifndef SBJ_Interfaces_IExceptionObserver_h
#define SBJ_Interfaces_IExceptionObserver_h

#include <boost/shared_ptr.hpp>

#include <stdexcept>
#include <string>

namespace SBJ
{

/*
Class: IExceptionObserver

Use IExceptionObserver if you want to supply a strategy for collecting
exceptions.
*/

class IExceptionObserver
{
public:
	typedef boost::shared_ptr< IExceptionObserver > Ptr;

	virtual ~IExceptionObserver( void ) {}

	void onException( const std::string& context = std::string() )
	{
		onException( std::runtime_error( "unknown" ), context );
	}

	void onException( const char* context )
	{
		if ( context != 0 )
		{
			onException( std::runtime_error( "unknown" ), context );
		}
		else
		{
			onException( std::runtime_error( "unknown" ) );
		}
	}

	virtual void onException( const std::exception& exception, const std::string& context = std::string() ) = 0;
};

/*
Class: DefaultExceptionObserver

DefaultExceptionObserver simply rethrows the exception
*/

class DefaultExceptionObserver : public IExceptionObserver
{
public:
	typedef boost::shared_ptr< IExceptionObserver > Ptr;

	DefaultExceptionObserver( void ) {}
	virtual ~DefaultExceptionObserver( void ) {}
	virtual void onException( const std::exception& exception, const std::string& context = std::string() )
	{
		throw exception;
	}
};

}

#endif